package flc.ast.fragment;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.MovieDetailActivity;
import flc.ast.activity.SignActivity;
import flc.ast.adapter.WantWatchAdapter;
import flc.ast.adapter.WatchedAdapter;
import flc.ast.adapter.WatchingAdapter;
import flc.ast.bean.WatchedBean;
import flc.ast.databinding.FragmentBingeWatchBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BingeWatchFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BingeWatchFragment extends BaseNoModelFragment<FragmentBingeWatchBinding> {

    private WatchedAdapter watchedAdapter;
    private WantWatchAdapter wantWatchAdapter;
    private WatchingAdapter watchingAdapter;
    private int type;

    @Override
    protected int onCreate() {
        return R.layout.fragment_binge_watch;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.ivAdd.setOnClickListener(this);
        mDataBinding.tvWatched.setOnClickListener(this);
        mDataBinding.tvWantWatch.setOnClickListener(this);
        mDataBinding.tvWatching.setOnClickListener(this);

        mDataBinding.rvWatchedList.setLayoutManager(new LinearLayoutManager(mContext));
        watchedAdapter = new WatchedAdapter();
        mDataBinding.rvWatchedList.setAdapter(watchedAdapter);
        watchedAdapter.addChildClickViewIds(R.id.ivWatchedItemAgain);
        watchedAdapter.setOnItemClickListener(this);
        watchedAdapter.setOnItemChildClickListener(this);

        mDataBinding.rvWantWatchList.setLayoutManager(new LinearLayoutManager(mContext));
        wantWatchAdapter = new WantWatchAdapter();
        mDataBinding.rvWantWatchList.setAdapter(wantWatchAdapter);
        wantWatchAdapter.addChildClickViewIds(R.id.ivWantWatchItemBingeWatch);
        wantWatchAdapter.setOnItemClickListener(this);
        wantWatchAdapter.setOnItemChildClickListener(this);

        mDataBinding.rvWatchingList.setLayoutManager(new LinearLayoutManager(mContext));
        watchingAdapter = new WatchingAdapter();
        mDataBinding.rvWatchingList.setAdapter(watchingAdapter);
        watchingAdapter.addChildClickViewIds(R.id.ivWatchingItemAlready);
        watchingAdapter.setOnItemClickListener(this);
        watchingAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAdd://添加
                SignActivity.type = type;
                startActivity(SignActivity.class);
                break;
            case R.id.tvWatched://已看
                clearSelection();
                type = 0;
                getWatchedData();
                mDataBinding.tvWatched.setTypeface(null, Typeface.BOLD);
                mDataBinding.ivWatchedSel.setVisibility(View.VISIBLE);
                break;
            case R.id.tvWantWatch://想看
                clearSelection();
                type = 1;
                getWantWatchData();
                mDataBinding.tvWantWatch.setTypeface(null, Typeface.BOLD);
                mDataBinding.ivWantWatchSel.setVisibility(View.VISIBLE);
                break;
            case R.id.tvWatching://在看
                clearSelection();
                type = 2;
                getWatchingData();
                mDataBinding.tvWatching.setTypeface(null, Typeface.BOLD);
                mDataBinding.ivWatchingSel.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == watchedAdapter) {
            StkResBean bean = watchedAdapter.getItem(position).getBean();
            MovieDetailActivity.stkResBean = bean;
            startActivity(MovieDetailActivity.class);
        } else {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            MovieDetailActivity.stkResBean = bean;
            startActivity(MovieDetailActivity.class);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        List<WatchedBean> listSpWatched = AppSpUtils.getWatchedList();
        List<StkResBean> listSpWantWatch = AppSpUtils.getWantWatchList();
        List<StkResBean> listSpWatching = AppSpUtils.getWatchingList();
        if (listSpWatched == null) listSpWatched = new ArrayList<>();
        if (listSpWantWatch == null) listSpWantWatch = new ArrayList<>();
        if (listSpWatching == null) listSpWatching = new ArrayList<>();
        switch (view.getId()) {
            case R.id.ivWatchedItemAgain://再刷
                listSpWatching.add(0, watchedAdapter.getItem(position).getBean());
                AppSpUtils.saveWatchingList(listSpWatching);
                listSpWatched.remove(position);
                AppSpUtils.saveWatchedList(listSpWatched);
                clearSelection();
                type = 2;
                getWatchingData();
                mDataBinding.tvWatching.setTypeface(null, Typeface.BOLD);
                mDataBinding.ivWatchingSel.setVisibility(View.VISIBLE);
                break;
            case R.id.ivWantWatchItemBingeWatch://追剧
                listSpWatching.add(0, wantWatchAdapter.getItem(position));
                AppSpUtils.saveWatchingList(listSpWatching);
                listSpWantWatch.remove(position);
                AppSpUtils.saveWantWatchList(listSpWantWatch);
                clearSelection();
                type = 2;
                getWatchingData();
                mDataBinding.tvWatching.setTypeface(null, Typeface.BOLD);
                mDataBinding.ivWatchingSel.setVisibility(View.VISIBLE);
                break;
            case R.id.ivWatchingItemAlready://已看
                listSpWatched.add(0, new WatchedBean(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")), watchingAdapter.getItem(position)));
                AppSpUtils.saveWatchedList(listSpWatched);
                listSpWatching.remove(position);
                AppSpUtils.saveWatchingList(listSpWatching);
                getWatchingData();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (type == 0) {
            getWatchedData();
        } else if (type == 1) {
            getWantWatchData();
        } else if (type == 2) {
            getWatchingData();
        }
    }

    private void getWatchedData() {
        List<WatchedBean> listSp = AppSpUtils.getWatchedList();
        if (listSp != null && listSp.size() > 0) {
            watchedAdapter.setList(listSp);
            mDataBinding.rvWatchedList.setVisibility(View.VISIBLE);
            mDataBinding.rvWantWatchList.setVisibility(View.GONE);
            mDataBinding.rvWatchingList.setVisibility(View.GONE);
            mDataBinding.ivWatchNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvWatchedList.setVisibility(View.GONE);
            mDataBinding.rvWantWatchList.setVisibility(View.GONE);
            mDataBinding.rvWatchingList.setVisibility(View.GONE);
            mDataBinding.ivWatchNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getWantWatchData() {
        List<StkResBean> listSp = AppSpUtils.getWantWatchList();
        if (listSp != null && listSp.size() > 0) {
            wantWatchAdapter.setList(listSp);
            mDataBinding.rvWatchedList.setVisibility(View.GONE);
            mDataBinding.rvWantWatchList.setVisibility(View.VISIBLE);
            mDataBinding.rvWatchingList.setVisibility(View.GONE);
            mDataBinding.ivWatchNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvWatchedList.setVisibility(View.GONE);
            mDataBinding.rvWantWatchList.setVisibility(View.GONE);
            mDataBinding.rvWatchingList.setVisibility(View.GONE);
            mDataBinding.ivWatchNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getWatchingData() {
        List<StkResBean> listSp = AppSpUtils.getWatchingList();
        if (listSp != null && listSp.size() > 0) {
            watchingAdapter.setList(listSp);
            mDataBinding.rvWatchedList.setVisibility(View.GONE);
            mDataBinding.rvWantWatchList.setVisibility(View.GONE);
            mDataBinding.rvWatchingList.setVisibility(View.VISIBLE);
            mDataBinding.ivWatchNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvWatchedList.setVisibility(View.GONE);
            mDataBinding.rvWantWatchList.setVisibility(View.GONE);
            mDataBinding.rvWatchingList.setVisibility(View.GONE);
            mDataBinding.ivWatchNoData.setVisibility(View.VISIBLE);
        }
    }

    private void clearSelection() {
        mDataBinding.tvWatched.setTypeface(null, Typeface.NORMAL);
        mDataBinding.ivWatchedSel.setVisibility(View.INVISIBLE);
        mDataBinding.tvWantWatch.setTypeface(null, Typeface.NORMAL);
        mDataBinding.ivWantWatchSel.setVisibility(View.INVISIBLE);
        mDataBinding.tvWatching.setTypeface(null, Typeface.NORMAL);
        mDataBinding.ivWatchingSel.setVisibility(View.INVISIBLE);
    }

}